﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using ChBusiness.Dictionary;

public partial class WebControl_CitySearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtCity = new DataTable();
        dtCity = GetListFromDictionary.GetSelectOptions("区属");

        XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
        Response.ContentType = "text/xml";////最重要的啊...
        Response.AppendHeader("Cache-Control", "no-cache");
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 4;
        writer.IndentChar = ' ';
        writer.WriteStartDocument();
        System.Xml.XmlDocument lodoc = new XmlDocument();
        lodoc = transDataTabletoXml(dtCity);
        lodoc.WriteTo(writer);
        writer.Flush();
        Response.End();
        writer.Close();
    }

    public System.Xml.XmlDocument transDataTabletoXml(DataTable dtTable)
    {
        System.Xml.XmlDocument lxmlCity = new XmlDocument();
        System.Xml.XmlNode loroot = lxmlCity.CreateElement("resultlist");
        lxmlCity.AppendChild(loroot);
       
        foreach (DataRow drRow in dtTable.Rows)
        {
            string lsId = drRow["keycode"].ToString();
            string lsName = drRow["keyvalue"].ToString();

            System.Xml.XmlNode loNode = lxmlCity.CreateElement("item");

            loNode.Attributes.Append(lxmlCity.CreateAttribute("id")).Value = lsId;

            loNode.Attributes.Append(lxmlCity.CreateAttribute("name")).Value = lsName;

            loroot.AppendChild(loNode);

        }
        return lxmlCity;

    }
}
